
package com.be.beadmin.device.impl;

import com.be.beadmin.device.entity.SpareInfo;
import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.device.entity.SpareInfo_;
import com.be.beadmin.project.entity.Info;
import lombok.RequiredArgsConstructor;
import com.be.beadmin.device.mapper.SpareInfoMapperEx;
import com.be.beadmin.device.repository.SpareInfoRepository;
import com.be.beadmin.device.service.SpareInfoService;
import com.be.beadmin.device.dto.input.SpareInfoDto;
import com.be.beadmin.device.dto.output.SpareInfoEx;
import com.be.beadmin.device.mapper.SpareInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.device.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Order;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description 服务实现
* @author zty
* @date 2023-07-30
**/
@Service
@RequiredArgsConstructor
public class SpareInfoServiceImpl implements SpareInfoService {

    @Autowired
    private  SpareInfoRepository spareInfoRepository;
    @Resource
    private  SpareInfoMapper spareInfoMapper;
    @Resource
    private  SpareInfoMapperEx spareInfoExMapper;

    @Override
    public Page<SpareInfoEx> getSpareInfoByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<SpareInfo> page = spareInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(SpareInfo_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(SpareInfo_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<SpareInfoEx> page1 = page.map(spareInfoExMapper::toEntity);
            return page1;

    }

    @Override
    public void addSpareInfo(SpareInfoDto spareInfoDto) {
            SpareInfo result = spareInfoRepository.findSpareInfoByIdAndDelete(spareInfoDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("SpareInfo不存在,请重新查询");
            }
            SpareInfo spareInfo = spareInfoMapper.toEntity(spareInfoDto);
            spareInfoRepository.save(spareInfo);
    }

    @Override
    public void updateSpareInfo(SpareInfoDto spareInfoDto) {
            SpareInfo spareInfo = spareInfoRepository.findSpareInfoById(spareInfoDto.getId());
            spareInfo = spareInfoMapper.toEntity(spareInfoDto);
            spareInfoRepository.save(spareInfo);
    }


    @Override
    public void delSpareInfo(String id) {
        SpareInfo spareInfo = spareInfoRepository.findSpareInfoById(id);
        spareInfo.setDelete(1);
        spareInfoRepository.save(spareInfo);
    }

    @Override
    public void delSpareInfos (List<String> ids) { }

    //筛选接口
    @Override
    public SpareInfo getSpareInfoByWarehouseNameAndContractDate(String warehouseName, Date contractDate) {
        SpareInfo spareInfo = spareInfoRepository.findSpareInfoByWarehouseNameAndContractDateAndDelete(warehouseName, contractDate, 0);
        if (spareInfo == null) {
            throw new BeadminException("未找到该项目");
        }
        return spareInfo;
    }

}